package com.zzx.business.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzx.business.domain.BusinessSite;
import com.zzx.business.vo.BusinessSiteVo;
import com.zzx.common.core.domain.entity.SysDept;
import com.zzx.common.core.domain.entity.SysUser;
import com.zzx.common.exception.base.BaseException;
import com.zzx.system.mapper.SysDeptMapper;
import com.zzx.system.service.ISysDeptService;
import com.zzx.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SiteService {

    @Autowired
    private IBusinessSiteService businessSiteService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private ISysUserService userService;

    @Transactional
    public boolean save(BusinessSiteVo entity) {

        String type = entity.getType();
        SysDept dept = new SysDept();
        if("1".equals(type)){ //源头站点
            SysDept sysDept = deptMapper.checkDeptNameUnique(entity.getName(), 2L);
            if(sysDept == null){
                dept.setAncestors("0,1,2");
                dept.setParentId(2l);
                dept.setDeptName(entity.getName());
                int i = deptMapper.insertDept(dept);
            } else {
                throw new BaseException("站点已存在");
            }


        } else if ("2".equals(type)) { //收货站点
            SysDept sysDept = deptMapper.checkDeptNameUnique(entity.getName(), 3L);
            if(sysDept == null){
                dept.setAncestors("0,1,3");
                dept.setParentId(3l);
                dept.setDeptName(entity.getName());
                int i = deptMapper.insertDept(dept);
            } else {
                throw new BaseException("站点已存在");
            }

        }
        entity.setDeptId(dept.getDeptId());
        businessSiteService.save(entity);
        return true;
    }

    @Transactional
    public boolean updateById(BusinessSiteVo entity) {
        SysDept dept = deptMapper.selectDeptById(entity.getDeptId());
        if(dept != null){
            dept.setDeptName(entity.getName());
            deptMapper.updateDept(dept);
        }
        businessSiteService.updateById(entity);
        return true;
    }

    @Transactional
    public boolean removeByIds(List<Long> ids){
        if(ids != null && !ids.isEmpty()){
            for (Long id: ids){
                BusinessSite byId = businessSiteService.getById(id);
                LambdaQueryWrapper<SysUser> wrapper = Wrappers.<SysUser>lambdaQuery().eq(SysUser::getDeptId, byId.getDeptId());
                userService.remove(wrapper);
                deptService.checkDeptDataScope(byId.getDeptId());
                deptMapper.deleteDeptById(byId.getDeptId());
            }
            businessSiteService.removeByIds(ids);
        }
        return true;
    }
}
